package com.sl.core.engine.rowset;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.sl.core.engine.datasource.DataSourceManager;
import com.sl.core.engine.datasource.domain.dto.DataSourceDTO;
import com.sl.core.engine.trans.ProcessTrans;
import org.anyline.entity.DataRow;
import org.anyline.entity.DataSet;
import org.anyline.metadata.Column;
import org.anyline.metadata.Table;
import org.anyline.metadata.type.init.StandardTypeMetadata;

import java.util.Map;
import java.util.StringJoiner;

public class ProcessDbRowSet extends ProcessAbstractDbRowSet {

    private DataSourceDTO dataSource;

    public ProcessDbRowSet(ProcessTrans processTrans) {
        super(processTrans);

    }

    public void initDataSource() {
        initDataSource(processInstance.getRowSetTagDataSourceId());
    }

    public void initDataSource(String dsCode) {

        if (ObjectUtil.isNotNull(dataSource)) {
            return;
        }
        synchronized (this) {
            if (ObjectUtil.isNotNull(dataSource)) {
                return;
            }
            DataSourceDTO dataSourceById = DataSourceManager.INSTANCE.getDataSourceById(dsCode);
            this.dataSource = dataSourceById;
        }
    }

    @Override
    public String typeCode() {
        return ProcessRowSetEnum.DB.getCode();
    }

    @Override
    protected String getJdbcUserName() {
        initDataSource();
        return dataSource.getUsername();
    }

    @Override
    protected String getJdbcPassword() {
        initDataSource();
        return dataSource.getPassword();
    }

    @Override
    protected String getJdbcUrl() {
//        AnylineServiceAdaptor anylineServiceAdaptor = processInstance.getAnylineServiceAdaptor();
//        AnylineServiceProxy anyLineService = anylineServiceAdaptor.getAnyLineService(processInstance.getTid(), processInstance.getRowSetTagDataSourceId());
        initDataSource();
        return dataSource.getUrl();
    }

    @Override
    protected String getJdbcDriver() {
        initDataSource();
        return dataSource.getDriverClassName();
    }

    @Override
    protected String getTableName() {
        return "process_db_row_set";
    }

    @Override
    protected void customColumns(Table table) {
        Column column = new Column("fromNode");
        column.setTypeMetadata(StandardTypeMetadata.VARCHAR);
        column.setLength(250);
        table.addColumn(column);

        column = new Column("toNode");
        column.setTypeMetadata(StandardTypeMetadata.VARCHAR);
        column.setLength(250);
        table.addColumn(column);
    }

    @Override
    protected void customPutRow(DataRow dataRow) {
        String originTransformName = this.getOriginTransformName();
        String destinationTransformName = this.getDestinationTransformName();
        dataRow.put("fromNode", originTransformName);
        dataRow.put("toNode", destinationTransformName);
    }

    protected String getWhereCondition() {
        String originTransformName = this.getOriginTransformName();
        String destinationTransformName = this.getDestinationTransformName();
        return " fromNode = '" + originTransformName + "' and toNode = '" + destinationTransformName + "'";
    }


    @Override
    public Map<String, Object> searchStepDataList(String date, String processId, String processVersion, String tid, String dbFile,
                                                  String source,
                                                  String target) {

        initDataSource();
        super.initDb();

        StringJoiner where = new StringJoiner(" ");
        where.add("1=1");

        if (StrUtil.isNotBlank(tid)) {
            where.add("and tid ='" + tid + "'");
        }
        if (StrUtil.isNotBlank(source)) {
            where.add("and fromNode ='" + source + "'");
        }
        if (StrUtil.isNotBlank(target)) {
            where.add("and toNode ='" + target + "'");
        }
        String tableName = getTableName();
        DataSet dataRows = anylineService.querys(tableName, where);
        Map<String, Object> result = Maps.newHashMap();
        result.put("type", typeCode());
        result.put("data", dataRows.getRows());
        return result;
    }
}
